package com.example.learn;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;

import androidx.annotation.NonNull;
import androidx.appcompat.widget.AppCompatTextView;
import androidx.recyclerview.widget.RecyclerView;

import java.util.ArrayList;

public class TagsAdapter extends RecyclerView.Adapter<TagsAdapter.ViewHolder> {

    private final ArrayList<TagsData> arrayList;
    private OnItemClickListener onItemClickListener = null;

    public TagsAdapter(ArrayList<TagsData> arrayList) {

        this.arrayList = arrayList;

    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {

        TagsData bean = arrayList.get(position);

        holder.titleTextView.setText(bean.getTitle());
        holder.urlTextView.setText(bean.getUrl());

        holder.mainRelativeLayout.setOnClickListener(view -> {
            if (onItemClickListener != null) {
                onItemClickListener.onClick(position, bean);
            }
        });

        holder.deleteTextView.setOnClickListener(view -> {
            if (onItemClickListener != null) {
                onItemClickListener.onDelete(position, bean);
            }
        });

    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup group, int viewType) {

        View view = LayoutInflater.from(group.getContext()).inflate(R.layout.item_tags, group, false);
        return new ViewHolder(view);

    }

    @Override
    public int getItemCount() {

        return arrayList.size();

    }

    @SuppressWarnings("unused")
    public void setOnItemClickListener(OnItemClickListener listener) {

        this.onItemClickListener = listener;

    }

    @SuppressWarnings("unused")
    public interface OnItemClickListener {

        @SuppressWarnings({"unused", "EmptyMethod"})
        void onClick(int position, TagsData bean);

        @SuppressWarnings({"unused", "EmptyMethod"})
        void onDelete(int position, TagsData bean);

    }

    static class ViewHolder extends RecyclerView.ViewHolder {

        private final RelativeLayout mainRelativeLayout;
        private final AppCompatTextView titleTextView;
        private final AppCompatTextView urlTextView;
        private final AppCompatTextView deleteTextView;

        private ViewHolder(View view) {

            super(view);
            mainRelativeLayout = view.findViewById(R.id.mainRelativeLayout);
            titleTextView = view.findViewById(R.id.titleTextView);
            urlTextView = view.findViewById(R.id.urlTextView);
            deleteTextView = view.findViewById(R.id.deleteTextView);

        }

    }

}
